<?php
class PapiWS_TestController extends Ext_Controller_Action
{
	protected $_requiresSecurity = false;
	protected $_requiresAuth = false;


	function saveproductAction() {

        $prod = new PapiWS_Model_PersonalizedProductVO();
        $prod->name = 'producto de prueba.';
        $prod->personalizedProductId = 111;
        $prod->faces = array();

        // levantamos el template 
        $tplService = new PapiWS_Service_ProductTemplate();
        $prod->templateProduct = $tplService->getInfo(10);

		$service = new PapiWS_Service_PersonalizedProduct();
		$ret = $service->saveProduct( $prod );

		print_r($ret);
		die();

        $client = new Zend_Soap_Client( 'http://soa:8001/PapiWS/personalizedproduct/soap?wsdl' );
		try {
    		$r = $client->saveProduct( $prod );
		} catch ( Exception $e ) {
			print_r( $client->getLastResponse() );
			print_r($e);die();
		}

		print_r($r);

		die('ok');
	}

	public function testsaveAction()
	{
		$service = new PapiWS_Service_PersonalizedProduct();

		$productId = $this->getRequest()->getParam('personalizedProductId');
		if ( ! $productId ) {
			die( 'falta especificar el persoanlizedProductId' );
		}
	
		$prod = $service->getProduct( $productId );
		$prod->name = 'Mi producto';

        // generamos las faces
        $f1 = new PapiWS_Model_PersonalizedFaceVO();
        $f1->faceOrder = 1;
        $f1->graphicalObjects = array();

        $f2 = new PapiWS_Model_PersonalizedFaceVO();
        $f2->faceOrder = 2;
        $f2->graphicalObjects = array();

        // agregamos un fondo a la primera cara
        $bg1 = new PapiWS_Model_BackgroundVO();
        $bg1->imageUrl = 'http://donline.ext.com.ar/sites/donline/files/gi4_0.jpg';
        $bg1->thumbnailUrl = 'http://donline.ext.com.ar/sites/donline/files/gi4.jpg';
        $bg1->type = PapiWS_Model_BackgroundVO::BACKGROUND_IMAGE;
        $bg1->x       = -180;
        $bg1->y       = -25;
        $bg1->width   = 266;
        $bg1->height  = 200;
        $bg1->fixedZ  = true;
        $f1->graphicalObjects[] = $bg1;

        // agregamos un fondo naranajaa la segunda cara
        $bg2 = new PapiWS_Model_BackgroundVO();
        $bg2->type = PapiWS_Model_BackgroundVO::BACKGROUND_FILL;
        $bg2->color = 0xff6600;
        $f2->graphicalObjects[] = $bg2;

        // agreguemos una shape a la parte de atrás
        $sh = new PapiWS_Model_ShapeVO();
        $sh->pngUrl = 'http://donline.ext.com.ar/sites/donline/files/icono5.png';
        $sh->swfUrl = 'http://donline.ext.com.ar/sites/donline/files/icono5.swf';
        $sh->svgUrl = 'http://donline.ext.com.ar/sites/donline/files/icono5.svg';
        $sh->x      = 50;
        $sh->y      = 60;
        $sh->z      = 1;
        $sh->width  = 128;
        $sh->height = 264;
        $f2->graphicalObjects[] = $sh;

        // agrego las dos caras
		$prod->faces = array();
        $prod->faces[] = $f1;
        $prod->faces[] = $f2;

		$ret = $service->saveProduct( $prod );

		print_r($ret);
		die();
	}

	public function testcreateAction()
	{
		$service = new PapiWS_Service_PersonalizedProduct();
		$ret = $service->createProduct( 10 );

		print_r($ret);
		die();
	}

	public function testcreatesessionAction()
	{
		$token = trim( $this->getRequest()->getParam( 'token' ) );
		if ( ! $token || strlen($token) < 32 ) {
			die( 'falta especificar el token o es muy corto' );
		}
	
		$service = new PapiWS_Service_PersonalizedProduct();
		$ret = $service->createSessionProduct( $token, 10 );

		print_r($ret);
		die();
	}

	public function testloadAction()
	{
		$productId = $this->getRequest()->getParam('personalizedProductId');
		$pp = new PapiWS_Service_PersonalizedProduct();
		if ( ! $productId ) {
			print_r($pp->getProduct( 202 ));
		} else {
			print_r($pp->getProduct( $productId ));
		}
		exit; 
	}

	public function testloadsessionAction()
	{
		$token = $this->getRequest()->getParam('token');
		$pp = new PapiWS_Service_PersonalizedProduct();
		print_r($pp->getProductForSession( $token ));
		exit; 
	}
	
	public function shapeAction()
	{
		require( 'Shape.php');
		$shape = new Shape();
		$res = $shape->getAll( 63 );
		print_r($res);
		exit;
	}
	
	public function productAction()
	{
		require( 'ProductTemplate.php');
		$product = new ProductTemplate();
		$res = $product->getInfo( 86 );
		print_r($res);
		exit;
	}

	public function backgroundAction()
	{
		require( 'Shape.php');
		$background = new Background();
		$res = $background->getAllCategories();

		if( count( $res )) {
			echo $res[0]['name'];
		}
		
		exit;
	}
	
	public function apiAction()
	{
		//$server = new Zend_Rest_Server();
		$server = new Zend_XmlRpc_Server();
		require_once( 'Testservice.php' );
		
		$server->setClass('Testservice');
		//$server->setProduction(false);
		echo $server->handle();
		exit;
	}	
	
	public function testwsAction()
	{
		require_once( 'vo/SimpleObject.php' );
		
		//$client = new Zend_Rest_Client('http://personalizationapi.dev:8001/test/api');
		$client = new Zend_XmlRpc_Client('http://personalizationapi.dev:8001/test/api');
		
		$obj = new SimpleObject();
		$obj->name = "test object.";
		$obj->id = 123454;
		//$client->createObject($obj);
		var_dump( $client->call('Testservice.createObject', array($obj)) );
		exit;
	}
}
